﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Markup;

namespace Vision.Core.ActivityComponents.Encryption.PGP
{
    /// <summary>
    /// 
    /// </summary>
    [ContentProperty("Body")]
    [Designer(typeof(PgpEncryptDesigner))]
    public class PgpEncrypt : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public InArgument<byte[]> PublicKey { get; set; }

		/// <summary>
		/// 
		/// </summary>
		[Category("Input")]
		public InArgument<string> PublicKeyUser { get; set; }
                
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public InArgument<string> UnencryptedFileName { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        [Category("Input")]
        public InArgument<byte[]> UnencryptedFileData {get; set;}

		/// <summary>
		/// 
		/// </summary>
		[Category("Input")]
		[DefaultValue(true)]
		public InArgument<bool> WithIntegrityPacket { get; set; }

		/// <summary>
        /// 
        /// </summary>
        [Category("Output")]
        public OutArgument<byte[]> EncryptedFileData { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DefaultValue(null)]
        public ActivityAction<byte[]> Body { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public PgpEncrypt()
        {
            this.Body = new ActivityAction<byte[]>()
            {
                Argument = new DelegateInArgument<byte[]>()
                {
                    Name = "encryptedFileData"
                }
            };

			this.WithIntegrityPacket = new InArgument<bool>(true);
        }

		/// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            using (MemoryStream unencryptedFileStream = new MemoryStream(this.UnencryptedFileData.Get(context)))
            using (MemoryStream publicKeyStream = new MemoryStream(this.PublicKey.Get(context))){
				byte[] bytes = null;
				if (string.IsNullOrEmpty(this.PublicKeyUser.Get(context)))
					bytes = PGPWrapper.EncryptStream(this.UnencryptedFileName.Get(context), this.WithIntegrityPacket.Get(context), unencryptedFileStream, publicKeyStream);
				else
					bytes = PGPWrapper.EncryptStream(this.UnencryptedFileName.Get(context), this.WithIntegrityPacket.Get(context), unencryptedFileStream, publicKeyStream, this.PublicKeyUser.Get(context));
                this.EncryptedFileData.Set(context, bytes);

                context.ScheduleAction<byte[]>(this.Body, bytes);
            }
        }
    }
}
